package cn.com.customer.manager.ui.regist

import android.os.Bundle
import androidx.core.content.ContextCompat
import cn.com.customer.manager.R
import cn.com.customer.manager.application.App
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.bean.RequestBean
import cn.com.customer.manager.http.BaseApi.IAuthorization
import cn.com.customer.manager.http.subscriber.TipRequestSubscriber
import com.xuexiang.xhttp2.XHttp
import com.xuexiang.xutil.tip.ToastUtils
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_find_pwd.*
import org.reactivestreams.Subscriber
import org.reactivestreams.Subscription
import java.util.concurrent.TimeUnit


open class FindPasswordActivity : BaseActivity() {
    var parameters: HashMap<String, String> =HashMap()
    var mSubscription: Subscription? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_find_pwd//To change body of created functions use File | Settings | File Templates.
    }

    override fun initView(savedInstanceState: Bundle?) {
        initActionBar(getString(R.string.update_pwd))

        button_submit.setOnClickListener {
            modifyPassWord()
        }
        tv_get_code.setOnClickListener {
             timer()
        }
    }

    private fun timer() {
        val count = 59L
        Flowable.interval(0, 1, TimeUnit.SECONDS)//设置0延迟，每隔一秒发送一条数据
                .onBackpressureBuffer()//加上背压策略
                .take(count) //设置循环次数
                .map{ aLong ->
                    count - aLong
                }
                .observeOn(AndroidSchedulers.mainThread())//操作UI主要在UI线程
                .subscribe(object : Subscriber<Long> {
                    override fun onSubscribe(s: Subscription?) {
                        tv_get_code.isEnabled = false//在发送数据的时候设置为不能点击
                        tv_get_code.setTextColor(ContextCompat.getColor(App.getContext(),R.color.gray_text))//背景色设为灰色
                        mSubscription = s
                        s?.request(Long.MAX_VALUE)//设置请求事件的数量，重要，必须调用
                    }
                    override fun onNext(aLong: Long?) {

                        tv_get_code.text = "${aLong}s" //接受到一条就是会操作一次UI
                    }
                    override fun onComplete() {
                        tv_get_code.text = getString(R.string.find_paw_again)
                        tv_get_code.isEnabled = true
                        tv_get_code.setTextColor(ContextCompat.getColor(App.getContext(),R.color.colorPrimary))
                        mSubscription?.cancel()//取消订阅，防止内存泄漏
                    }
                    override fun onError(t: Throwable?) {
                        t?.printStackTrace()
                    }
                })
    }

    private fun modifyPassWord() {
        val phone = et_phone.text.toString().trim()
        val code = et_code.text.toString().trim()
        val pass = et_pass.text.toString().trim()
        val confirmPass = et_confirm_pass.text.toString().trim()
        val oldPass = old_pass.text.toString().trim()
//        if(phone.isNullOrEmpty()){
//            ToastUtils.toast(resources.getString(R.string.input_phone))
//            return
//        }
//        if(code.isNullOrEmpty()){
//            ToastUtils.toast(resources.getString(R.string.input_vliate_code))
//            return
//        }
        if(oldPass.isNullOrEmpty()){
            ToastUtils.toast(resources.getString(R.string.input_pwd_format))
            return
        }
        if(pass.isNullOrEmpty()){
            ToastUtils.toast(resources.getString(R.string.find_pwd_intput_pass))
            return
        }
        if(confirmPass.isNullOrEmpty()){
            ToastUtils.toast(resources.getString(R.string.input_confirm_pwd))
            return
        }

        if (pass.length<6||pass.length>12){
            ToastUtils.toast(R.string.input_pwd_format)
            return
        }
        if (confirmPass.length<6||confirmPass.length>12){
            ToastUtils.toast(R.string.input_pwd_format)
            return
        }
        if (confirmPass!=pass){
            ToastUtils.toast(R.string.find_paw_noSame)
            return
        }
        parameters["oldPassoword"] = oldPass
        parameters["newPassword"] = confirmPass

        val request = XHttp.custom()
        request.call(request.create(IAuthorization::class.java)
                .modifyPassword(RequestBean(parameters)))
                .subscribeWith(object : TipRequestSubscriber<Map<String,String>?>() {
                    override fun onSuccess(apiResult: Map<String,String>?) {
                        apiResult?.let {
                            finish()
                        }
                    }
                })
    }
}